package com.naiterui.ehp.bs.cms.modules.base.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.constants.CacheConstants;
import com.naiterui.ehp.bs.cms.common.constants.DsName;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.entity.Hospital;
import com.naiterui.ehp.bs.cms.modules.base.mapper.HospitalBaseMapper;
import com.naiterui.ehp.bs.cms.modules.base.service.IHospitalService;
import com.naiterui.ehp.bs.cms.modules.base.vo.HospitalPageVO;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author gaoliangliang
 * @date 2021/2/24 下午3:35
 **/
@Slf4j
@Service
@DS(DsName.EHP)
public class HospitalServiceImpl extends ServiceImpl<HospitalBaseMapper, Hospital> implements IHospitalService {
    @Autowired
    private HospitalBaseMapper hospitalMapper;

    @Override
    public PageVO<HospitalPageVO> queryPage(PageParamsVO pageParamsVO, HospitalPageVO hospitalPageVO) {
        QueryWrapper<HospitalPageVO> entityWrapper = buildEntityWrapper(pageParamsVO, hospitalPageVO);

        Page<HospitalPageVO> page = baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);

        return PageVO.toPageVo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @CacheEvict(value = CacheConstants.HOSPITAL, allEntries = true)
    public void saveOrUpdate(HospitalPageVO hospitalPageVO) throws BusinessException {
        Hospital hospital = new Hospital();
        BeanUtil.copyProperties(hospitalPageVO, hospital);
        // 判断同城市是否存在重名停用医院
        int stopCount = hospitalMapper.getHospitalIsStopByName(hospital);
        if (stopCount > 0) {
            // throw new RuntimeException("同城市存在该医院名称并已停用");
            throw new BusinessException(BaseExceptionCodes.DATA_ERR, "同城市存在该医院名称并已停用");
        }
        // 判断同城市是否存在该医院名称
        int nameCount = hospitalMapper.getHospitalByName(hospital);
        if (nameCount > 0) {
            // throw new RuntimeException("同城市已存在该医院名称,请修改");
            throw new BusinessException(BaseExceptionCodes.DATA_ERR, "同城市已存在该医院名称");
        }
        if (hospital.getId() != null) {
            baseMapper.updateById(hospital);
        } else {
            baseMapper.insert(hospital);
        }
    }

    @Override
    public void updateHospitalStatus(Long hospitalId, Integer status) {
        hospitalMapper.updateHospitalStatus(hospitalId, status);
    }

    @Override
    public HospitalPageVO queryByHospitalId(Long hospitalId) {

        return hospitalMapper.getById(hospitalId);
    }

    private QueryWrapper<HospitalPageVO> buildEntityWrapper(PageParamsVO pageParamsVO, HospitalPageVO hospitalPageVO) {
        QueryWrapper<HospitalPageVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
        Long id = hospitalPageVO.getId();
        entityWrapper.eq(id != null, "t.id", id);
        Integer levelType = hospitalPageVO.getLevelType();
        entityWrapper.eq(levelType != null, "t.level_type", levelType);
        Integer hide = hospitalPageVO.getHide();
        entityWrapper.eq(hide != null, "t.hide", hide);
        Integer cityId = hospitalPageVO.getCityId();
        entityWrapper.eq(cityId != null, "t.city_id", cityId);
        String name = hospitalPageVO.getName();
        entityWrapper.like(StringUtils.isNotBlank(name), "t.name", name);
        return entityWrapper;
    }
}
